﻿namespace PropOnline.Helpers
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;
    using System.Web.Routing;
    using PropOnline.ViewModels.Shared;
    using PropOnline.Stores;

    public static class BusquedaHelper
    {                
        public static RouteValueDictionary GetBusquedaRouteValues(BusquedaViewModelBase busquedaViewModel)
        {                        
            RouteValueDictionary routeValues = new RouteValueDictionary(busquedaViewModel);            

            PropertyInfo barriosOptionsPropertyInfo = busquedaViewModel.GetType().GetProperties().Where(p => p.PropertyType == typeof(IList<BarrioOption>)).FirstOrDefault();

            if (barriosOptionsPropertyInfo == null)
            {
                return routeValues;
            }

            IList<BarrioOption> barriosOptions = barriosOptionsPropertyInfo.GetValue(busquedaViewModel, null) as IList<BarrioOption>;

            if (barriosOptions == null)
            {
                return routeValues;
            }

            routeValues.Remove("BarriosOptions");

            var i = 0;
            foreach (BarrioOption barrio in barriosOptions)
            {
                if (barrio.IsChecked || barrio.HasHijosChecked)
                {                    
                    routeValues.Add(string.Format(CultureInfo.InvariantCulture, "BarriosOptions[{0}].Id", i), barrio.Id);
                    routeValues.Add(string.Format(CultureInfo.InvariantCulture, "BarriosOptions[{0}].IsChecked", i), barrio.IsChecked);

                    var j = 0;
                    foreach (BarrioOption hijo in barrio.Hijos)
                    {
                        if (hijo.IsChecked)
                        {
                            routeValues.Add(string.Format(CultureInfo.InvariantCulture, "BarriosOptions[{0}].Hijos[{1}].Id", i, j), hijo.Id);
                            routeValues.Add(string.Format(CultureInfo.InvariantCulture, "BarriosOptions[{0}].Hijos[{1}].IsChecked", i, j), hijo.IsChecked);
                            j++;
                        }
                    }

                    i++;
                }                                                                              
            }

            return routeValues;
        }

        public static RouteValueDictionary GetFilterRouteValues(string ambientes = "", string superficie = "", string moneda = "", string precioMin = "", string precioMax = "", string tiempoEnMercado = "")
        {            
            var filteredRouteValues = new 
            {
                Ambientes = string.IsNullOrEmpty(ambientes) ? null : ambientes,                                
                Superficie = string.IsNullOrEmpty(superficie) ? null : superficie,
                PrecioMin = string.IsNullOrEmpty(precioMin) ? null : precioMin,
                PrecioMax = string.IsNullOrEmpty(precioMax) ? null : precioMax,                
                Moneda = string.IsNullOrEmpty(moneda) ? null : moneda,
                MesesEnMercado = string.IsNullOrEmpty(tiempoEnMercado) ? null : tiempoEnMercado,                
            };

            return new RouteValueDictionary(filteredRouteValues);
        }

        public static IList<BarrioOption> FillBarriosOptions(IEnumerable<BarrioOption> barriosOptionsQueryString)
        {
            var barriosOptions = new BusquedaStore().Barrios.ToList();

            if (barriosOptionsQueryString != null)
            {
                barriosOptions.ForEach(a =>
                {
                    a.IsChecked = barriosOptionsQueryString.Any(b => b.Id == a.Id && b.IsChecked);

                    a.Hijos.ForEach(h =>
                    {
                        h.IsChecked = barriosOptionsQueryString.SelectMany(p => p.Hijos.Where(q => q.Id == h.Id)).Any(p => p.IsChecked);
                    });
                });
            }

            return barriosOptions;
        }
    }
}
